--
-- Author: ping.wang
-- Date: 2019-05-21 16:34:16
--
-- DailyRoutinePanel
--

local ClassRef = app.mvc.createPanelClass(...)
local ListViewVertical = require("app.widget.listview.ListViewVertical")
local DailyRoutineRender = import("..views.renders.DailyRoutineRender")
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.DailyRoutinePanel))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._mCdMask = self._ui.Panel_left:findChild("loading")
    self._mCdMask:setVisible(false)
    self._progressBar = cc.ProgressTimer:create( self._mCdMask )
    self._progressBar:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
    self._progressBar:setScaleX(self._mCdMask:getScaleX())
    self._progressBar:setScaleY(self._mCdMask:getScaleY())
    self._progressBar:setMidpoint(cc.p( 0.5, 0.5 ))
    self._progressBar:setPosition(self._mCdMask:getPosition())
    self._ui.Panel_left:addChild(self._progressBar)

    self._ui.Panel_left:findChild("bg1"):setLocalZOrder(2)

    self._progressBar:setPercentage(0)
    self._tlt = self._ui.Panel_left:findChild("tlt")
    self._progressText = self._ui.Panel_left:findChild("num")
    self._progressText:setString(string.format(L("dailyroutine5"),0))


    self._dailyRoutines = {}
    local configData = {}
    if self._data == 1 then
        self._tlt:setString(L("dailyroutine11"))
        configData = GD:getDailyRoutineData()
    else
        self._tlt:setString(L("dailyroutine12"))
        configData = GD:getWeekRoutineData()
    end

    --数据要根据是否开放排个序 未开放的放后面
    local notOpen = {}
    for k,v in ipairs(configData) do
        if tonumber(v.openLevel) <= UD:getLevel() then
            table.insert(self._dailyRoutines,{conf = v})
        else
            table.insert(notOpen, v)
        end
    end

    for k,v in ipairs(notOpen) do
        table.insert(self._dailyRoutines, {conf = v, islock = true})
    end


    local classifyPanel = nil
    self._iconOvers = {}
    self._currentClassifyFlag = {true,true,true,true}
    for i = 1, 4 do
        classifyPanel = self._ui.Panel_left:findChild(string.format("data_%d",i))
        classifyPanel:setTouchEnabled(true)
        classifyPanel:findChild("name"):setString(L("dailyroutine"..tostring(i)))
        classifyPanel:addClickEventListener(
        function()
            self:_classifyRoutine(i)
        end)
        table.insert(self._iconOvers, classifyPanel:findChild("icon_over"))
        self._iconOvers[i]:setVisible(true)
    end

    --TODO 初始化全部显示

    local params = {
        renderClass = DailyRoutineRender,
        perNum = 4,
        gapX = 0,
        paddingLeft = 0,
        paddingRight = 0,
        cellEventProxy = function(eventName, data)
            dump(" jump to ")
        end,
    }

    self._productListnode = ListViewVertical.new( self._ui.Panel:findChild("ScrollView"), params )
    self._productListnode:reloadData(self._dailyRoutines)

    local rewardModel = app:getInst("RewardModel")
    local heroTreasureActivity = rewardModel:getHeroTreasureActivity()
    local yxbzPanel = self._ui.Panel_left:findChild("btn_yxbz")
    if heroTreasureActivity then
        yxbzPanel:setVisible(self._data == 2)
        yxbzPanel:addClickEventListener(function()
            --跳转到活动
            app:sendMsg("RewardController", "openView", heroTreasureActivity)
        end)
    else
        yxbzPanel:setVisible(false)
    end

    self:setContentSize( self._ui.Panel:getContentSize() )
end

--服务器发送过来每个活动的具体信息还包括是否已经完成 我们要根据完成与否排序
function ClassRef:_sortRoutineData()

end

function ClassRef:_classifyRoutine(idx)
    if idx == 1 and self._currentClassifyFlag[2] and self._currentClassifyFlag[3] and self._currentClassifyFlag[4] then
        return
    end

    if idx == 1 and not self._currentClassifyFlag[1] then
        for i = 1, 4 do
            self._currentClassifyFlag[i] = true
            self._iconOvers[i]:setVisible(true)
        end
        --选择全部 TODO
        self._productListnode:reloadData(self._dailyRoutines)
    else
        self._currentClassifyFlag[idx] = not self._currentClassifyFlag[idx]
        self._iconOvers[idx]:setVisible(self._currentClassifyFlag[idx])

        if self._currentClassifyFlag[2] and self._currentClassifyFlag[3] and self._currentClassifyFlag[4] then
            self._currentClassifyFlag[1] = true
            self._iconOvers[1]:setVisible(true)
        else
            self._currentClassifyFlag[1] = false
            self._iconOvers[1]:setVisible(false)
        end

        --过滤部分则是当前 2 3 4中选择为true的部分 当然如果都没有也是可能的
        self._productListnode:reloadData(self:getRoutineDatasByBelong())
    end
end

function ClassRef:getRoutineDatasByBelong()
    local allFlag = false
    for k,v in ipairs(self._currentClassifyFlag) do
        if v then
            allFlag = true
            break
        end
    end

    if not allFlag then
        return {}
    else
        local val = {}
        for k,v in ipairs(self._dailyRoutines) do
            local vBelongs = string.split(v.conf.belong,";")
            for i = 1, #vBelongs do
                if self._currentClassifyFlag[tonumber(vBelongs[i]) + 1] then
                    table.insert(val,v)
                    break
                end
            end
        end
        return val
    end
end

function ClassRef:getTestData()
    local test = {Progress = 7234, ModuleInfos={}}
    for i = 1, 12 do
        table.insert(test.ModuleInfos, {ID = i,IntPar = i, Max = i+1, State = (i%2 == 0)})
    end
    return test
end

--服务器传递来的数据{ModuleInfos = {{ID = 1,IntPar = 1,Max = 2,State = false}},Progress = 7000}
function ClassRef:refreshDataFromServer(data)
    --收到服务器发来的数据 根据数据排序
    self._dailyRoutines = {}

    self._progressBar:setPercentage(math.floor(data.Progress/10)/10)
    self._progressText:setString(string.format(L("dailyroutine5"),math.floor(data.Progress/10)/10))

    for k,v in ipairs(data.ModuleInfos) do
        local conf = nil
        if self._data == 1 then
            conf = GD:getDailyRoutineDataById(v.ID)
        else
            conf = GD:getWeekRoutineDataById(v.ID)
        end

        if conf then
            table.insert(self._dailyRoutines, {conf = conf,IntPar = v.IntPar,Max = v.Max,State = v.State, islock = (tonumber(conf.openLevel) > UD:getLevel())})
        end
    end

    --要排序
    table.sort(self._dailyRoutines,function(a,b)
        if a.State ~= b.State then
            return b.State
        end

        if a.islock ~= b.islock then
            return b.islock
        end

        return tonumber(a.conf.order) < tonumber(b.conf.order)
    end)

    self._productListnode:reloadData(self:getRoutineDatasByBelong())
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef